# -*- coding: utf-8 -*-
# @Time    : 2024/8/26 14:55
# @Author  : CHNJX
# @File    : report_sum.py
# @Desc    :
import csv
from datetime import datetime
import re


def parse_date(date_string):
    return datetime.strptime(date_string, '%Y-%m-%d')


def extract_model(device_info):
    return device_info.split('_')[0]


def extract_test_case(coverage_case):
    match = re.search(r'\((.*?)\)', coverage_case)
    return match.group(1) if match else coverage_case


def generate_summary(start_date, end_date, input_file, output_file):
    start = parse_date(start_date)
    end = parse_date(end_date)

    summary = {}

    with open(input_file, 'r', encoding='utf-8') as f:
        reader = csv.DictReader(f, delimiter='\t')
        for row in reader:
            test_date = parse_date(row['测试日期'])
            if start <= test_date <= end:
                model = extract_model(row['设备信息'])
                test_case = extract_test_case(row['覆盖用例'])

                if model not in summary:
                    summary[model] = {'用例': set(), '测试次数': 0}

                summary[model]['用例'].add(test_case)
                summary[model]['测试次数'] += 1

    with open(output_file, 'w', encoding='utf-8', newline='') as f:
        writer = csv.writer(f)
        writer.writerow(['型号', '用例', '测试次数', '测试结果'])

        for model, data in summary.items():
            writer.writerow([
                model,
                '、'.join(data['用例']),
                data['测试次数'],
                ''  # 测试结果留空
            ])


# 使用示例
start_date = '2024-08-23'
end_date = '2024-08-26'
input_file = 'paste.txt'  # 确保这个文件存在并包含了您提供的数据
output_file = 'test_summary.csv'

generate_summary(start_date, end_date, input_file, output_file)
print(f"Summary has been generated and saved to {output_file}")